package com.example.demo.dao;

import com.example.demo.entity.Comment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Map;

/**
 * 评论的数据库操作类
 */
public interface CommentDao extends JpaRepository<Comment,Integer> {

    @Query(value = "SELECT " +
            "c.*, " +
            "u.nickname, " +
            "u.avatar," +
            "(select nickname from user where id = (select user_id from comment where id = (SELECT reply_id  from comment where id = c.id)))as replyNickname "+
            "FROM " +
            "comment c " +
            "JOIN user u ON c.user_id = u.id " +
            "WHERE " +
            "c.circle_id = ?1",nativeQuery = true)
    List<Map<String,Object>> getList(int circleId);
    @Query(value = "SELECT " +
            "c.*, " +
            "u.nickname, " +
            "u.avatar, " +
            "(select nickname from user where id = (select user_id from comment where id = (SELECT reply_id  from comment where id = c.id)))as replyNickname "+
            "FROM " +
            "comment c " +
            "JOIN user u ON c.user_id = u.id " +
            "WHERE " +
            "c.id = ?1",nativeQuery = true)
    Map<String,Object> getOne(int id);
}
